package net.scrumtool.controller;

import net.scrumtool.model.Collaborator;
import net.scrumtool.model.Product;
import net.scrumtool.model.Sprint;
import net.scrumtool.technics.DAO;
import net.scrumtool.technics.Utils;
import net.scrumtool.view.Connexion;
import net.scrumtool.view.Home;
import net.scrumtool.view.OverviewProduct;
import net.scrumtool.view.OverviewSprint;
import net.scrumtool.view.Profile;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;

public class MainController {
	Home view;
	private MenuItem productOpenFile;

	public MainController(Home view) {
		this.view = view;
	}

	static Collaborator userActif;

	public static void main(String[] args) {

		// OUVERTURE DU PROGRAMME
		if (userActif == null) {
			Connexion co = new Connexion();
			co.init();

			Cconnexion cc = new Cconnexion(co);
			cc.init();

			co.open();
		} else {

			Home home = new Home();
			home.getLblInfoUser().setText("hello");
			home.init();

		}

	}

	public void init(final Home home) {
		final Cdashboard db = new Cdashboard(home);
		final Cproducts prod = new Cproducts(home);
		final Csprint sprint = new Csprint(home);
		final Ccollaborators col = new Ccollaborators(home);
		db.init(userActif);
		db.listener(userActif);
		prod.listener(userActif);
		sprint.listener(userActif);
		col.listener();

		
		//TEST SI ADMIN OU NON
		if(userActif.getAdministrator()==false){
			view.getTabFolder().getItem(1).dispose();
			view.getTabFolder().getItem(1).dispose();
			view.getTabFolder().getItem(1).dispose();
			view.getCascadeMenuAddFile().setEnabled(false);
		}
		
		
		// AFFICHE NOM ET PRENOM DU MEMBRE CONNECTE
		view.getLblInfoUser().setText("Hello " + userActif.getFirstname() + " " + userActif.getLastname());

		// AFFICHE LE MESSAGE DE BIENVENUE
		view.getLblWelcomeDashboard().setText("Welcome " + userActif.getFirstname() + ", to SCRUMTOOL.");

		// TRAITEMENT TABCONTROL PAGE HOME

		view.getTabFolder().addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent event) {

				switch (view.getTabFolder().getSelection()[0].getText()) {

				case "Dashboard":
					db.init(userActif);
					break;
				case "Products": // FLORIAN
					prod.init();
					break;
				case "Sprints": // ANTHONY
					if (DAO.getProducts().size() == 0) {
						prod.init();
						view.getTabFolder().setSelection(1);
						Utils.MsgBox(view.getShell(), "Please first create a project", "information");

					} else {
						sprint.init();
					}
					break;
				case "Collaborators": // JORDI
					col.init();
					break;
				default:
					break;
				}

			}
		});

		// MENU HOME

		for (final Product productOpen : DAO.getProducts()) {
			MenuItem mntmNewItem = new MenuItem(view.getMenuOpenFileProduct(), SWT.NONE);
			mntmNewItem.setText("" + productOpen.getName());
			mntmNewItem.addSelectionListener(new SelectionListener() {

				@Override
				public void widgetSelected(SelectionEvent evt) {
					MessageBox messageBoxproduct = new MessageBox(view.getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO);
					messageBoxproduct.setText("SCRUMTOOL");
					messageBoxproduct.setMessage("Do you want to open this projet ?");
					int responseProduct = messageBoxproduct.open();
					if (responseProduct == SWT.YES) {
						OverviewProduct overview = new OverviewProduct();
						overview.init();
						CoverviewProduct cOverview = new CoverviewProduct(overview);
						cOverview.listener(userActif, null);
						cOverview.init(userActif, productOpen);
						view.close();
						overview.open();
					}
				}

				@Override
				public void widgetDefaultSelected(SelectionEvent arg0) {
					// TODO Auto-generated method stub

				}
			});
		}

		for (final Sprint sprintOpen : DAO.getSprints()) {
			MenuItem mntmNewItem = new MenuItem(view.getMenuOpenFileSprint(), SWT.NONE);
			mntmNewItem.setText("" + sprintOpen.getLabel());
			mntmNewItem.addSelectionListener(new SelectionListener() {

				@Override
				public void widgetSelected(SelectionEvent evt) {
					MessageBox messageBoxproduct = new MessageBox(view.getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO);
					messageBoxproduct.setText("SCRUMTOOL");
					messageBoxproduct.setMessage("Do you want to open this sprint ?");
					int responseProduct = messageBoxproduct.open();
					if (responseProduct == SWT.YES) {
						OverviewSprint overview = new OverviewSprint();
						overview.init();
						CoverviewSprint cOvS = new CoverviewSprint(overview);
						cOvS.listener(userActif);
						cOvS.init(sprintOpen);
						view.close();
						overview.open();
					}
				}

				@Override
				public void widgetDefaultSelected(SelectionEvent arg0) {
					// TODO Auto-generated method stub

				}
			});
		}

		view.getItemMenuProductFile().addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent event) {
				prod.init();
				view.getTabFolder().setSelection(1);
				view.getTxtNameProject().setEditable(true);
				view.getTltmEditProduct().setEnabled(false);
				view.getTltmDelProduct().setEnabled(false);
				view.getTltmOpenProduct().setEnabled(false);
				view.getTltmEditCollaboratorsProduct().setEnabled(false);
				view.getGrpProject().setText("Add product");
				view.getTxtNameProject().setText("");
				view.getTxtNameProject().setMessage("Exemple : My Project");
				view.getTxtDescriptionProduct().setText("");
				view.getBtnProducts().setImage(Utils.getImage(Utils.IMG_ADD));
				view.getBtnProducts().setText("Add this project");
				view.getGrpProject().setVisible(true);
				view.getTxtNameProject().setEditable(true);
				view.getTxtDescriptionProduct().setEditable(true);
				view.getBtnProducts().setVisible(true);

				if (view.getTabFolder().getSelection().equals(view.getTabFolder().getSelection()[0])) {

				}

			}
		});

		view.getItemMenuSprintFile().addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent event) {
				sprint.init();
				view.getTabFolder().setSelection(2);
				view.getTltmEditSprints().setEnabled(false);
				view.getTltmDeleteSprints().setEnabled(false);
				view.getTltmOpenSprints().setEnabled(false);
				view.getCbProductsSprints().setEnabled(true);
				view.getGrpSprint().setVisible(true);
				view.getGrpSprint().setEnabled(true);
				view.getTxtNameSprint().setText("");
				view.getBtnSprint().setImage(Utils.getImage(Utils.IMG_ADD));
				view.getBtnSprint().setText("Add this sprint");
				view.getBtnSprint().setVisible(true);

			}
		});

		view.getItemMenuCollaboratorFile().addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent event) {
				col.init();
				view.getTabFolder().setSelection(3);
				view.getTltmEditCollaborator().setEnabled(false);
				view.getTltmDelCollaborator().setEnabled(false);
				view.getGrpCollaborators().setText("Add collaborator");
				view.getGrpCollaborators().setVisible(true);
				view.getBtnCollaborators().setImage(Utils.getImage(Utils.IMG_ADD));
				view.getBtnCollaborators().setText("Add this collaborator");
				view.getTxtFirstnameCollaborators().setText("");
				view.getTxtLastNameCollaborators().setText("");
				view.getTxtEmailCollaborators().setText("");
				view.getTxtPasswordCollaborators().setText("");
				view.getTxtConfirmPasswordCollaborators().setText("");
				view.getTxtFirstnameCollaborators().setEditable(true);
				view.getTxtLastNameCollaborators().setEditable(true);
				view.getTxtEmailCollaborators().setEditable(true);
				view.getTxtPasswordCollaborators().setEditable(true);
				view.getTxtConfirmPasswordCollaborators().setEditable(true);
				view.getBtnCollaborators().setVisible(true);
			}
		});

		view.getMenuOpenFileSignOut().addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent event) {
				MessageBox messageBox = new MessageBox(view.getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO);
				messageBox.setText("SCRUMTOOL");
				messageBox.setMessage("Are you sure you want to exit ?");
				int response = messageBox.open();
				if (response == SWT.YES) {
					Connexion connexion = new Connexion();
					connexion.init();
					Cconnexion cConnexion = new Cconnexion(connexion);
					cConnexion.init();
					view.close();
					connexion.open();
				}
			}
		});

		view.getItemMenuAboutScrumtool().addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent event) {
				Utils.MsgBox(view.getShell(), "Product name : SCRUMTOOL\nVersion: beta\n\nThis software is distributed for free. Developed within a framework of study, it permit the production of scrum project. The software is optimized so as to facilitate his use. He is currently in trial version. Our team is currently planning on a more stable version.\n\nProject members : \n\nRomain Bertrand\nFlorian Briens\nJordi Ferte\nAnthony Gautreau\n\nSCRUMTOOL \u00a9 2014 TEAMSIO", "information");
			}
		});

		view.getLinkProfile().addMouseListener(new MouseListener() {

			@Override
			public void mouseDoubleClick(
					org.eclipse.swt.events.MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseDown(org.eclipse.swt.events.MouseEvent arg0) {
				Profile profile = new Profile();
				profile.init();
				Cprofile cProfile = new Cprofile(profile);
				cProfile.listener(userActif, home.getLblInfoUser());
				cProfile.init(userActif);
				profile.open();

			}

			@Override
			public void mouseUp(org.eclipse.swt.events.MouseEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		view.getLinkLogout().addMouseListener(new MouseListener() {

			@Override
			public void mouseDoubleClick(
					org.eclipse.swt.events.MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseDown(org.eclipse.swt.events.MouseEvent arg0) {

				MessageBox messageBox = new MessageBox(view.getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO);
				messageBox.setText("SCRUMTOOL");
				messageBox.setMessage("Are you sure you want to exit ?");
				int response = messageBox.open();
				if (response == SWT.YES) {
					Connexion connexion = new Connexion();
					connexion.init();
					Cconnexion cConnexion = new Cconnexion(connexion);
					cConnexion.init();
					view.close();
					connexion.open();
				}
			}

			@Override
			public void mouseUp(org.eclipse.swt.events.MouseEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

	}

}
